#pragma once

#include "noncopyable.h"

#include <functional>
#include <string>
#include <vector>
#include <memory>

class EventLoop;
class EventLoopThread;

class EventLoopThreadPool : noncopyable
{
public:
    using ThreadInitCallback = std::function<void(EventLoop*)>;

    EventLoopThreadPool(EventLoop* baseLoop, const std::string& nameArg);
    ~EventLoopThreadPool();

    void setThreadNum(int numThreads) { numThreads_ = numThreads; }

    void start(const ThreadInitCallback& cb = ThreadInitCallback());
    
    // 如果工作在多线程，baseLoop_(相当于mainloop了）会默认以轮询的方式分配channel给subloop
    EventLoop* getNextLoop();

    std::vector<EventLoop*> getAllLoops();

    bool start() const { return started_; }

    const std::string& name() const { return name_; }
private:
    EventLoop* baseLoop_;  // EventLoop loop，用户创建的那个最开始的loop
    std::string name_;
    bool started_;
    int numThreads_;
    int next_;  // 轮询下标

    std::vector<std::unique_ptr<EventLoopThread>> threads_;  // 包含所有创建EventLoop的线程
    std::vector<EventLoop*> loops_;  // 包含具体创建出的EventLoop的指针
};